package indi.zhifa.expshare.class001.chemicalmatch.service.impl;

import com.alibaba.excel.EasyExcel;
import indi.zhifa.expshare.class001.chemicalmatch.dao.IChemicalCompositionRepository;
import indi.zhifa.expshare.class001.chemicalmatch.entity.dto.excel.ChemicalCompositionExcelData;
import indi.zhifa.expshare.class001.chemicalmatch.exel.ChemicalCompositionListener;
import indi.zhifa.expshare.class001.chemicalmatch.service.IChemicalCompatibleService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RequiredArgsConstructor
@Component
public class MaterialServiceImpl implements IChemicalCompatibleService {

    final MongoTemplate mMongoTemplate;
    final IChemicalCompositionRepository mChemicalCompositionRepository;
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void readExcel(MultipartFile pFileRequest) throws IOException {

        ChemicalCompositionListener listener = new ChemicalCompositionListener(
                mMongoTemplate,mChemicalCompositionRepository);
        EasyExcel.read(
                pFileRequest.getInputStream(),
                ChemicalCompositionExcelData.class,listener)
                .sheet("化学兼容性")
                .doRead();
    }

    @Override
    public void clear() {
        mMongoTemplate.remove(new Query(),"material");
    }
}
